﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyStorage;

namespace ElectricBillMaker
{
    public class Model
    {
        private IModelChangeEvents events_;
        private IDatabase db_;

        public int Delta1 {get;set;}
        public int Delta2 { get; set; }
        public int Delta3 { get; set; }

        public float Tarif1 { get; set; }
        public float Tarif2 { get; set; }
        public float Tarif3 { get; set; }

        public float ToPay1 { get; set; }
        public float ToPay2 { get; set; }
        public float ToPay3 { get; set; }

        public float Registration1 { get; set; }
        public float Registration2 { get; set; }
        public float Registration3 { get; set; }

        public Registration CurrentRegistration { get; set; }
        public Registration PreviousRegistration { get; set; }

        public int PrivilegeKw { get; set; }
        public int PrivilegePercent { get; set; }

        public DateTime MonthYear { get; set; }
        

        public Model( IModelChangeEvents events, IDatabase db )
        {
            events_ = events;
            db_ = db;
        }

        public void Calculate()
        {
            ToPay1 = Delta1 >= PrivilegeKw ? 
                Convert.ToSingle( ((Delta1 - PrivilegeKw) * Tarif1) 
                                                + PrivilegeKw * Tarif1 * (Convert.ToSingle(PrivilegePercent) / 100) )
                                                : Convert.ToSingle(Delta1 * Tarif1 * (Convert.ToSingle(PrivilegePercent) / 100));
            events_.OnToPay1Change(ToPay1);
            ToPay2 = Convert.ToSingle( Delta2 * Tarif2 );
            events_.OnToPay2Change(ToPay2);
            ToPay3 = Convert.ToSingle( Delta3 * Tarif3 );
            events_.OnToPay3Change(ToPay3);

            events_.OnCalculated(Convert.ToSingle(ToPay1 + ToPay2 + ToPay3));
        }

        public void OnBill()
        {
            MyStorage.Registration registration = new MyStorage.Registration();
            registration.Tarif1 = Tarif1;
            registration.Tarif2 = Tarif2;
            registration.Tarif3 = Tarif3;
            registration.Registration1 = Registration1;
            registration.Registration2 = Registration2;
            registration.Registration3 = Registration3;
            registration.MonthYear = MonthYear;
            registration.PrivilegeKw = PrivilegeKw;
            registration.PrivilegePercent = PrivilegePercent;
            registration.FillDate = DateTime.Now;

            db_.AddRegistration(registration);
            db_.SubmitChanges();
        }

    }
}

